﻿/* Copyright © 2008 by Marcus Bengtsson - makkbe@gmail.com   
 * 
 * This file is part of TaskDispatcher.
 *
 * TaskDispatcher is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * TaskDispatcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TaskDispatcher.  If not, see <http://www.gnu.org/licenses/>.
 **/

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Dispatcher.Logging
{
    public sealed class Logger
    {
        private static Logger instance = null;
        private readonly String eventSource = "Dispatcher";
        private readonly String eventLog = "Application";

        /// <summary>
        /// Returns an instance of Logger.
        /// </summary>
        public static Logger Instance
        {
            get
            { 
                if(instance == null)
                {
                    instance = new Logger();
                }
                
                return instance;
            }
        }

        /// <summary>
        /// Private constructor for Singleton pattern.
        /// </summary>
        private Logger()
        {
            if (!EventLog.SourceExists(eventSource))
            {
                try
                {
                    EventLog.CreateEventSource(eventSource, eventLog);
                }

                catch (Exception e)
                {
                    //Catch them all and let God sort them out...
                }
            }
        }

        /// <summary>
        /// Log a message to the default Event Log.
        /// </summary>
        /// <param name="eventMessage">The message to log</param>
        /// <param name="eventType">The EventLogEntryType to use</param>
        public void Log(String eventMessage, EventLogEntryType eventType)
        {
            try
            {
                EventLog.WriteEntry(this.eventSource, eventMessage, eventType);
            }

            catch (Exception e)
            {
                //Catch them all and let God sort them out...
            }
        }

        /// <summary>
        /// Log an exception to the default Event Log.
        /// </summary>
        /// <param name="e"></param>
        public void Log(Exception e)
        {
            try
            {
                EventLog.WriteEntry(this.eventSource, e.Message, EventLogEntryType.Error);
            }

            catch (Exception ex)
            {
                //Catch them all and let God sort them out...
            }
        }
    }
}
